
package StudyBuddy;
import java.io.*;
import java.net.*;
import javax.swing.*;

/**
 *
 * @author Gabriel
 */


public class Plugin{

private Object newclass;
private Runnable plugin;
private String name;
private File pluginlocation;
private int thescore;
private boolean loaded = false; // This will tell if the plugin is loaded

    public Plugin(File newfile){
        
        // The plugin constructor does the job of loading the constructor.
        // If any errors are found, it throws an exception        
       /* 
        pluginlocation = newfile;
        
        try {
            File temp = newfile.getParentFile().getParentFile();
            URL url = temp.toURL();          
            URL[] urls = new URL[]{url};
            ClassLoader cl = new URLClassLoader(urls);
            Class cls = cl.loadClass(newfile.getParentFile().getName() + "." + newfile.getName().subSequence(0, newfile.getName().length() - 6).toString());
            newclass = cls.newInstance();
            System.out.println(newclass.toString());
            
        } catch (MalformedURLException e) {
                JOptionPane.showMessageDialog(null,"The URL is Malformed. But since it's generated by toURL you should never see this Error");
        } catch (ClassNotFoundException e) {
                JOptionPane.showMessageDialog(null,"Class not found Exception - This should never be thrown unless you deleted the class file at the precise moment it's loaded ... UNLIKELY ... or you're hacking into the computer not cool");
        }catch (IOException e) {
                JOptionPane.showMessageDialog(null,"General IO Exception - Figure the problem out yourself");
        } catch (InstantiationException e) {
                JOptionPane.showMessageDialog(null,"Instantiation Exception - Class may not be a class");
        }catch (IllegalAccessException e) {
                JOptionPane.showMessageDialog(null,"Illegal Access Exception - Class may be in use");
        }
        
        if (newclass instanceof RunnableBuddy){
            RunnableBuddy temp = (RunnableBuddy)newclass;
//            System.out.println("Runnable Buddy");
            name = temp.getName();
        }
        else{
            name = newfile.toString();
        }
         */     
    }
    
    public void loadPlugin(File newfile) throws Exception{
        
        pluginlocation = newfile;
        
        
        File temp = newfile.getParentFile().getParentFile();
        URL url = temp.toURL();          
        URL[] urls = new URL[]{url};
        ClassLoader cl = new URLClassLoader(urls);
        Class cls = cl.loadClass(newfile.getParentFile().getName() + "." + newfile.getName().subSequence(0, newfile.getName().length() - 6).toString());
        newclass = cls.newInstance();
        System.out.println(newclass.toString());
            
        /*
        catch (MalformedURLException e) {
                JOptionPane.showMessageDialog(null,"The URL is Malformed. But since it's generated by toURL you should never see this Error");
                return false;
        } catch (ClassNotFoundException e) {
                JOptionPane.showMessageDialog(null,"ClassNotFoundException \nThis means quite simply that the \"class\" was not found.\nThe file you're trying to open is NOT A CLASS!");
                return false;
        }catch (IOException e) {
                JOptionPane.showMessageDialog(null,"General IO Exception - Figure the problem out yourself");
                return false;
        } catch (InstantiationException e) {
                JOptionPane.showMessageDialog(null,"Instantiation Exception - Class may not be a class");
                return false;
        }catch (IllegalAccessException e) {
                JOptionPane.showMessageDialog(null,"Illegal Access Exception - Class may be in use");
                return false;
        }
        */
        if (newclass instanceof RunnableBuddy){
            RunnableBuddy temp2 = (RunnableBuddy)newclass;
//            System.out.println("Runnable Buddy");
            name = temp2.getName();
        }
        else{
            name = newfile.toString();
        }

        loaded = true;
        
    }
    
    public void runplugin(){
        // Checks to see if plugin is runnable
        // And the runs the plugin
        // May want to decouple runplugin and logininfo ... 

        if (newclass instanceof RunnableBuddy){        
             LoginInfo newLoginInfo = new LoginInfo();
            ((RunnableBuddy)newclass).setLoginInfo(newLoginInfo.getUsernameInfo());
        }     
        
        if (newclass instanceof Runnable){
            
            plugin = (Runnable)newclass;
            plugin.run();
        }     
         
   }
    
    public String getName(){
     
        if (newclass instanceof RunnableBuddy){
            RunnableBuddy temp = (RunnableBuddy)newclass;
            return temp.getName();
        }
        else{
            return name;
        }
        
        
    }
    
    public int getScore(){
        
        if (newclass instanceof RunnableBuddy){

            RunnableBuddy temp = (RunnableBuddy)newclass; 
            return temp.getScore();
            
        }
        
        return 0;
    }
   
    public String toString(){
        
        // Returns the plugin location
        
        return pluginlocation.toString();
        
    }
   
    
}
